package cn.getech.data.development.service.impl;

import cn.getech.data.development.entity.UploadVersionLog;
import cn.getech.data.development.entity.UploadVersionResultLog;
import cn.getech.data.development.mapper.UploadVersionLogMapper;
import cn.getech.data.development.mapper.UploadVersionResultLogMapper;
import cn.getech.data.development.model.dto.UploadVersionResultLogDto;
import cn.getech.data.development.model.vo.VersionStatusResultVO;
import cn.getech.data.development.service.UploadVersionResultLogService;
import cn.getech.data.intelligence.common.exception.RRException;
import cn.getech.data.intelligence.common.utils.PojoUtils;
import cn.getech.data.intelligence.common.utils.R;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class UploadVersionResultLogServiceImpl extends ServiceImpl<UploadVersionResultLogMapper, UploadVersionResultLog> implements UploadVersionResultLogService {

    @Autowired
    private UploadVersionLogMapper uploadVersionLogMapper;
    @Autowired
    private UploadVersionResultLogMapper uploadVersionResultLogMapper;

    @Override
    public List<UploadVersionResultLogDto> addList(List<VersionStatusResultVO> restList, String versionUUid) {
        if (CollectionUtil.isNotEmpty(restList)) {
            UploadVersionLog uploadVersionLog = uploadVersionLogMapper.selectOne(new QueryWrapper<UploadVersionLog>().eq("zip_path_name", versionUUid));
            if (null != uploadVersionLog) {
                //先删除再添加
                uploadVersionResultLogMapper.delete(new QueryWrapper<UploadVersionResultLog>().eq("upload_version_log_id",uploadVersionLog.getId()));
                List<UploadVersionResultLogDto> retList = new ArrayList<>();
                Map<Integer, List<VersionStatusResultVO>> typeMaps = restList.stream().collect(Collectors.groupingBy(VersionStatusResultVO::getErrorType));
                if (CollectionUtil.isNotEmpty(typeMaps)) {
                    for (Map.Entry<Integer, List<VersionStatusResultVO>> entry : typeMaps.entrySet()) {
                        Integer mapKey = entry.getKey();
                        List<VersionStatusResultVO> mapValue = entry.getValue();
                        UploadVersionResultLog uploadVersionResultLog = new UploadVersionResultLog();
                        uploadVersionResultLog.setUploadVersionLogId(uploadVersionLog.getId());
                        if (CollectionUtil.isNotEmpty(mapValue)) {
                            long successedCount = mapValue.stream().filter(o -> Objects.equals(o.getErrorNeedType(), 2)).count();
                            uploadVersionResultLog.setSumCount((long) mapValue.size());
                            uploadVersionResultLog.setFailCount(mapValue.size() - successedCount);
                            uploadVersionResultLog.setSuccessCount(successedCount);
                        }
                        uploadVersionResultLog.setResultType(mapKey);
                        if (CollectionUtil.isNotEmpty(mapValue)) {
                            uploadVersionResultLog.setRestMsgList(JSONObject.toJSONString(mapValue));
                        }
                        uploadVersionResultLogMapper.insert(uploadVersionResultLog);
                        UploadVersionResultLogDto uploadVersionResultLogDto = new UploadVersionResultLogDto();
                        BeanUtils.copyProperties(uploadVersionResultLog, uploadVersionResultLogDto);
                        retList.add(uploadVersionResultLogDto);
                    }
                }
                return retList;
            }
        }
        return null;
    }

    @Override
    public List<UploadVersionResultLogDto> listByUploadLogId(Long id) {
        UploadVersionLog uploadVersionLog = uploadVersionLogMapper.selectById(id);
        if (null == uploadVersionLog) {
            throw new RRException("版本上传记录不存在！");
        }
        if (!Objects.equals(uploadVersionLog.getUpZipStatus(), 0) && !Objects.equals(uploadVersionLog.getUpZipStatus(), 1)) {
            throw new RRException("版本上传记录未导入,不能查看结果！");
        }
        List<UploadVersionResultLog> uploadVersionResultLogs = uploadVersionResultLogMapper.selectList(new QueryWrapper<UploadVersionResultLog>().eq("upload_version_log_id", id));
        List<UploadVersionResultLogDto> uploadVersionResultLogDtos = PojoUtils.listConvert(UploadVersionResultLogDto.class, uploadVersionResultLogs);
        return pingRestList(uploadVersionResultLogDtos);
    }

    @Override
    public List<UploadVersionResultLogDto> pingRestList(List<UploadVersionResultLogDto> uploadVersionResultLogDtos) {
        List<UploadVersionResultLogDto> list = new ArrayList<>();
        if (CollectionUtil.isEmpty(uploadVersionResultLogDtos)) {
            initErrorTypeUploadVersionResultLog(1, list);
            initErrorTypeUploadVersionResultLog(2, list);
            initErrorTypeUploadVersionResultLog(3, list);
            initErrorTypeUploadVersionResultLog(4, list);
            initErrorTypeUploadVersionResultLog(5, list);
            initErrorTypeUploadVersionResultLog(6, list);
        } else {
            List<Integer> allTypes = Arrays.asList(1, 2, 3, 4, 5, 6);
            List<Integer> haveTypes = uploadVersionResultLogDtos.stream().map(UploadVersionResultLogDto::getResultType).collect(Collectors.toList());
            for (Integer type : allTypes) {
                if (!haveTypes.contains(type)) {
                    initErrorTypeUploadVersionResultLog(type, list);
                }else{
                    list.add(uploadVersionResultLogDtos.stream().filter(o -> Objects.equals(o.getResultType(),type)).findAny().orElse(null));
                }
            }
        }
        return list;
    }

    private void initErrorTypeUploadVersionResultLog(int errorType, List<UploadVersionResultLogDto> uploadVersionResultLogs) {
        UploadVersionResultLogDto uploadVersionResultLogDto = new UploadVersionResultLogDto();
        uploadVersionResultLogDto.setSumCount(0L);
        uploadVersionResultLogDto.setFailCount(0L);
        uploadVersionResultLogDto.setSuccessCount(0L);
        uploadVersionResultLogDto.setResultType(errorType);
        uploadVersionResultLogs.add(uploadVersionResultLogDto);
    }
}
